import logging
import pickle
from pathlib import Path

from tools.page import page_get
from tools.处理excel import List


def 快递单号获取(date):
    if Path(f'D:\\快递赔付\\{date}\\data.pkl').exists():
        logging.info(f'{date}快递揽收时效统计已存在，跳过')
        return
    page = page_get()
    logging.info(f'开始获取{date}快递揽收时效统计')
    page.set.download_path(f'D:\\快递赔付\\{date}')
    page.set.download_file_name('快递揽收时效统计.xlsx')
    page('text=快递揽收信息').click(by_js=True)
    page.wait.ele_displayed('.btn_search', timeout=20)
    page.wait(3)
    page('#status').select('已发货')
    page('text=运输中').click(by_js=True)
    page('#begin_date').input(date, clear=True)
    page('#end_date').input(date, clear=True)
    page('#timeConsumingHour').input('72', clear=True)
    page('.btn_search').click(by_js=True)
    page.wait.ele_displayed('text=导出EXCEL', timeout=60)
    page.wait(3)
    page('text=导出EXCEL').click(by_js=True)
    page.wait.download_begin(timeout=120)
    page.wait.downloads_done(timeout=120)
    page.wait(3)
    logging.info(f'{date}快递揽收时效统计获取完成')
    dataList = List(f"D:\\快递赔付\\{date}\\快递揽收时效统计.xlsx")
    with open(f'D:\\快递赔付\\{date}\\data.pkl', 'wb') as file:
        pickle.dump(dataList, file)
